
package com.visikard.ui.activity;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import com.google.android.gcm.GCMBaseIntentService;
import com.visikard.ui.R;


/**
 * IntentService responsible for handling GCM messages.
 */
public class GCMIntentService extends GCMBaseIntentService {

	public static final String ANDROID_ID_NUM = "2";
	public static final String SENDER_ID = "465063224961";
	public static String regId = "";
	
	public static final int PUSH_COMMENT_FEED = 1;
	public static final int PUSH_POST_FEED = 2;
	public static final int PUSH_POST_MESSAGE = 3;
	public static final int PUSH_TRADE_KARD = 4;
	public static final int PUSH_COMMENT_MEDIA = 5;
	public static final int PUSH_POST_MEDIA = 6;
	public static final int PUSH_SHARE_KARD = 7;
	public static final int PUSH_ADD_NEW_ALBUM = 8;
	public static final int PUSH_BUY_AND_GIVE_GIFT_KARD = 9;
	public static final int PUSH_ADD_NEW_MEDIA_ALBUM = 10;
	
	
	public GCMIntentService() {
        super(SENDER_ID);        
    }

	@Override
	protected void onError(Context arg0, String arg1) {		
	}

	@Override
	protected void onMessage(Context arg0, Intent arg1) {			
		ItemData item = new ItemData(arg1);						
		
	}

	@Override
	protected void onRegistered(Context arg0, String arg1) {
		regId = arg1;
	}

	@Override
	protected void onUnregistered(Context arg0, String arg1) {
	}
	
	private static void generateFeedNotification(Context context, ItemData item ) {
		int icon = R.drawable.ic_stat_gcm;
        long when = System.currentTimeMillis();
        NotificationManager notificationManager = (NotificationManager)
                context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(icon, item.getMessage(), when);
        String title = context.getString(R.string.app_name);
        
        Intent notificationIntent = new Intent(context, MainHomeActivity.class);
        
        // set intent so it does not start a new activity
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent =
                PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        notification.setLatestEventInfo(context, title, item.getMessage(), intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        
        notificationManager.notify((int)when, notification);        
    }
	
	private class ItemData {
		int pushType, fkUser, fkKard, isPrivate;
		long idNews;
		String message;
		
		public ItemData( Intent it ) {								
			try{				
				pushType = Integer.parseInt(it.getStringExtra("pushType"));								
				
				fkUser = Integer.parseInt(it.getStringExtra("fkUser"));
				fkKard = Integer.parseInt(it.getStringExtra("fkKard"));
				
				if( pushType == PUSH_COMMENT_MEDIA ) {
					idNews = Long.parseLong(it.getStringExtra("idMedia"));
				} else {
					idNews = Long.parseLong(it.getStringExtra("idNews"));
				}
				
				isPrivate = Integer.parseInt(it.getStringExtra("isPrivate"));
				message = it.getStringExtra("message");				
			} catch (Exception e) { 
				pushType = -1;				
			}
		}

		public int getPushType() {
			return pushType;
		}

		public int getFkUser() {
			return fkUser;
		}

		public int getFkKard() {
			return fkKard;
		}

		public long getIdNews() {
			return idNews;
		}

		public int getIsPrivate() {
			return isPrivate;
		}

		public String getMessage() {
			return message;
		}
	}
}
